<template>
  <div>
    <el-row class="lock-btn">
      <el-button-group style="width:1000px">
        <el-button type="success" :disabled="userState" size="medium" v-if="roleIndex == false" @click="addBtn" round>添加</el-button>

        <el-input v-model="queryParams.name" style="width:300px;float: left;margin-left: 1%;" clearable placeholder="请输入停车场名称">
      </el-input>

      <el-button :disabled="userState" style="margin-left: 0.6%;margin-top: 0.2%;" size="medium" type="primary" @click="queryBtn" round>搜索</el-button>
      
      </el-button-group>
    </el-row>
    <el-row style="box-shadow: rgb(205 205 205) 1px 1px 8px;">
      <el-col :span="24">
        <el-table :header-cell-style="{ background: '#ecf3fd',color:'#000000' }" highlight-current-row ref="parkfieldTable" v-loading="loading" :data="parkfields" :stripe=true :row-key="'id'" :max-height="clientHeight" :height="clientHeight" @selection-change="handleTableChange">
 

          <el-table-column prop="unicode" label="停车场编号" width="120" align="center" sortable>
          </el-table-column>
          <el-table-column prop="name" label="停车场名称" width="260" align="center" sortable>
          </el-table-column>
          <el-table-column prop="address" label="地址" width="260" align="center">
          </el-table-column>
          <el-table-column prop="managerName" label="管理员名称" width="120" align="center">
          </el-table-column>
          <el-table-column prop="type" label="停车场类型" width="100" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.type == '0'" size="medium" type="success">扫码充电</el-tag>
              <el-tag v-else-if="scope.row.type == '1'" size="medium" type="info">相机联动</el-tag>
              <el-tag v-else-if="scope.row.type == '2'" size="medium" type="danger">纯充电桩</el-tag>
              <el-tag v-else-if="scope.row.type == '3'" size="medium" type="warning">纯泊控器</el-tag>
            </template>
          </el-table-column>

          <el-table-column prop="settlementPartnerName" label="结算账号" width="260" align="center"></el-table-column>
          
          <el-table-column prop="promotion" label="促销活动" width="100" align="center">
            <template slot-scope="scope">
              <el-button :disabled="userState" size="mini" type="primary" @click="editPromotionBtn(scope.$index, scope.row)">配置</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="promotion" label="监控" width="120" align="center">
            <template slot-scope="scope">
              <el-button :disabled="userState" size="mini" type="danger" @click="editMonitorBtn(scope.$index, scope.row)"><i class="el-icon-setting"></i></el-button>
            </template>
          </el-table-column>
          <el-table-column prop="dayFree" label="免费开始时间" width="70" align="center"></el-table-column>
          <el-table-column prop="nightFree" label="免费结束时间" width="70" align="center"></el-table-column>
          <el-table-column prop="lastSettlementDate" label="结算时间" v-if="roleIndex == false" width="200" align="center">
            <template slot-scope="scope">
              <el-date-picker 
                  v-if="cellEditIndexArray[scope.$index]"
                  v-model="scope.row.lastSettlementDate"
                  type="datetime"
                  placeholder="选择日期"
                  auto-complete="off">
                </el-date-picker>
                <el-button v-if="cellEditIndexArray[scope.$index]" icon="el-icon-check" circle style="margin-left: 0px;" @click="handleTimeSub(scope.$index, scope.row)"></el-button>
                <el-button v-if="cellEditIndexArray[scope.$index]" icon="el-icon-delete" circle style="margin-left: 0px;" @click="handleTimeCancel(scope.$index, scope.row)"></el-button>
              <span v-if="!cellEditIndexArray[scope.$index]" @click="handleTime(scope.$index, scope.row)">{{scope.row.lastSettlementDate}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="occupiedIssuedDate" label="占位生效时间" width="160" align="center">
          </el-table-column>
          <el-table-column prop="createDate" label="创建时间" width="160" align="center">
          </el-table-column>
          <el-table-column label="操作" v-if="roleIndex == false" width="140" align="center">
            <template slot-scope="scope">
              <el-button :disabled="userState" class="operateBtn" size="mini" type="primary" @click="editBtn(scope.$index, scope.row)" title="编辑"><i class="el-icon-edit-outline"></i></el-button>
              <el-button :disabled="userState" class="operateBtn" size="mini" type="danger" @click="delParkfield(scope.$index, scope.row)" title="删除"><i class="el-icon-delete"></i></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24" class="page-tool">
        <el-pagination layout="total,sizes,prev, pager,next,jumper" :page-size="pageSize" :total="total" :page-sizes="[10, 20, 30, 40, 100]" @size-change="handlePageChange" @current-change="handleNoChange">
        </el-pagination>
      </el-col>
    </el-row>
    <el-dialog :title="dialogTitle" :visible.sync="addDialog" :width="'422px'">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="停车场编号" :label-width="labelWidth" prop="unicode">
          <el-input v-model="form.unicode" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="停车场名称" :label-width="labelWidth" prop="name">
          <el-input v-model="form.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="停车场位置" :label-width="labelWidth" prop="address">
          <el-input v-model="form.address" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="经度" :label-width="labelWidth" prop="longitude">
          <el-input v-model="form.longitude" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="纬度" :label-width="labelWidth" prop="latitude">
          <el-input v-model="form.latitude" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="停车场类型" :label-width="labelWidth" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option v-for="type in types" v-bind:key="type.type" :label="type.typeName" :value="type.type"></el-option>  
          </el-select>
        </el-form-item>
        <el-form-item label="占位收费时间" :label-width="labelWidth" required>
          <template>
            <el-col :span="8">
              <el-form-item prop="dayFree">
                <el-select v-model="form.dayFree" placeholder="" @change="changeManager(form.dayFree)" style="width: 70px;">
                  <el-option v-for="hour in hours" v-bind:key="hour" :label="hour" :value="hour"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col class="line" :span="2">-</el-col>
            <el-col :span="8">
              <el-form-item prop="nightFree">
                <el-select v-model="form.nightFree" placeholder="" @change="changeManager(form.nightFree)" style="width: 70px;">
                  <el-option v-for="hour in hours" v-bind:key="hour" :label="hour" :value="hour"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </template>
        </el-form-item>
        <el-form-item label="权属人" :label-width="labelWidth" prop="managerId">
          <el-select v-model="form.managerId" placeholder="请选择权属人" @change="changeManager(form.managerId)" :disabled="selectDisable">
            <el-option v-for="manager in managers" v-bind:key="manager.id" :label="manager.loginName" :value="manager.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结算账号" :label-width="labelWidth" prop="settlementAccount">
          <el-select v-model="form.settlementAccount" placeholder="请选择结算账号" :disabled="selectDisable">
            <el-option v-for="account in accounts" v-bind:key="account.id" :label="account.name" :value="account.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="占位生效时间" :label-width="labelWidth" prop="occupiedIssuedDate">
          <el-date-picker
            v-model="form.occupiedIssuedDate"
            type="datetime"
            placeholder="选择日期"
            auto-complete="off">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" :label-width="labelWidth">
          <el-input v-model="form.remark" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialog = false">取 消</el-button>
        <el-button type="primary" @click="saveParkfield">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="promotionDialogTitle" :visible.sync="promotionDialog" :width="'422px'">
      <el-form :model="promotionForm" :rules="promotionRules" ref="promotionForm">
        <el-form-item label="活动名称" :label-width="labelWidth" prop="name">
          <el-input v-model="promotionForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <div v-for="(item, index) in promotionForm.acitivityContent" :key="index">
          <el-form-item label="预充金额" :label-width="labelWidth"
          :prop="'acitivityContent.'+ index + '.depositAmount'" 
          :rules="[{required: true, message: '请输入预充金额', trigger: 'blur'}]">
            <el-input v-model="item.depositAmount" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="赠送金额" :label-width="labelWidth"
          :prop="'acitivityContent.'+ index + '.bonusAmount'" 
          :rules="[{required: true, message: '请输入赠送金额', trigger: 'blur'}]">
            <el-input v-model="item.bonusAmount" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="描述" :label-width="labelWidth"
          :prop="'acitivityContent.'+ index + '.desc'" 
          :rules="[{required: true, message: '请输入描述', trigger: 'blur'}]">
            <el-input v-model="item.desc" auto-complete="off"></el-input>
          </el-form-item>
        </div>
        <el-form-item label="" :label-width="labelWidth">
          <el-button @click="addItem()">+</el-button>
          <el-button @click="deleteItem()">-</el-button>
        </el-form-item>
        <el-form-item label="生效日期" :label-width="labelWidth" prop="effectiveDate">
          <el-date-picker
            v-model="promotionForm.effectiveDate"
            type="datetime"
            placeholder="选择日期"
            auto-complete="off">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" :label-width="labelWidth" prop="deadlineDate">
          <el-date-picker
            v-model="promotionForm.deadlineDate"
            type="datetime"
            placeholder="选择日期"
            auto-complete="off">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="promotionDialog = false">取 消</el-button>
        <el-button v-if="!this.buttonHidden" type="danger" @click="delPromotionBtn">删 除</el-button>
        <el-button v-if="this.buttonHidden" type="primary" @click="savePromotionBtn">新 增</el-button>
        <el-button v-if="!this.buttonHidden" type="primary" @click="savePromotionBtn">修 改</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="monitorDialogTitle" :visible.sync="monitorDialog" :width="'422px'">
      <el-form :model="monitorForm" ref="monitorForm">
        <div v-for="(item, index) in monitorForm.monitorContent" :key="index">
          <el-form-item label="appkey" :label-width="labelWidth"
          :prop="'monitorContent.'+ index + '.appkey'" 
          :rules="[{required: true, message: '请输入appkey', trigger: 'blur'}]">
            <el-input v-model="item.appkey" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="secret" :label-width="labelWidth"
          :prop="'monitorContent.'+ index + '.secret'" 
          :rules="[{required: true, message: '请输入secret', trigger: 'blur'}]">
            <el-input v-model="item.secret" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="source" :label-width="labelWidth"
          :prop="'monitorContent.'+ index + '.source'" 
          :rules="[{required: true, message: '请输入secret', trigger: 'blur'}]">
            <el-input v-model="item.source" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="监控链接" :label-width="labelWidth"
          :prop="'monitorContent.'+ index + '.url'" 
          :rules="[{required: true, message: '请输入监控链接', trigger: 'blur'}]">
            <el-input v-model="item.url" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="品牌" :label-width="labelWidth"
          :prop="'monitorContent.'+ index + '.brand'" 
          :rules="[{required: true, message: '请选择品牌', trigger: 'change'}]">
            <el-select v-model="item.brand" placeholder="请选择品牌">
              <el-option :value="0" :key="0" label="海康"></el-option>
              <el-option :value="1" :key="1" label="大华"></el-option>
              <el-option :value="2" :key="2" label="科达"></el-option>
              <el-option :value="3" :key="3" label="其他"></el-option>
            </el-select>
          </el-form-item>
        </div>
        <el-form-item label="" :label-width="labelWidth" v-if="this.buttonHiddenMonitor">
          <el-button @click="addMonitorItem()">+</el-button>
          <el-button @click="deleteMonitorItem()">-</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="monitorDialog = false;buttonHiddenMonitor = false">取 消</el-button>
        <el-button v-if="!this.buttonHiddenMonitor" type="danger" @click="delMonitorBtn">删 除</el-button>
        <el-button v-if="this.buttonHiddenMonitor" type="primary" @click="saveMonitorBtn">新 增</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="playMonitorDialogTitle" :visible.sync="playMonitorDialog" :width="'422px'">
      
    </el-dialog>
  </div>
</template>
<script>
import {
  loadParkfieldApi,
  addParkfieldApi,
  delParkfieldApi,
  updateParkfieldApi,
  updateSettlementDateByParkfield
} from "../../api/parkfield.js";
import { loadManagerApi } from "../../api/manager.js";
import {  loadPartnerByManagerApi } from "../../api/partner.js";
import { getNickName, getTestUserName, getRoleId } from "../../util/auth";
import { getLcparkfieldpromotionApi, setLcparkfieldpromotionApi, delLcparkfieldpromotionApi } from "../../api/lcparkfieldpromotion.js";
import { getLcMonitorInfoApi, setLcMonitorInfoApi, delLcMonitorInfoApi } from "../../api/lcmonitorInfo.js";
 
export default {
  name: "runInfo",
  data: function () {
    return {
      clientHeight:
        Math.max(
          document.documentElement.scrollHeight,
          document.body.scrollHeight
        ) -
        132 -
        5 +
        10 -
        28,
      labelWidth: "120px",
      loading: false,
      dialogTitle: "",
      pageNo: 1,
      pageSize: 20,
      total: 0,
      multipleSelection: [],
      parkfields: [],
      queryParams:{
        name:""
      },
      form: {},
      addDialog: false,
      managers: [],
      promotionForm: {
        name: "", 
        desc: "", 
        effectiveDate: "", 
        deadlineDate: "", 
        depositAmount1: "", 
        bonusAmount1: "",
        depositAmount2: "", 
        bonusAmount2: "",
        depositAmount3: "", 
        bonusAmount3: "",
        depositAmount4: "", 
        bonusAmount4: "",
        acitivityContent: [],
      },
      promotionDialogTitle: "",
      promotionDialog: false,
      lcparkfieldpromotions: [],
      acitivityForm: {
        acitivityContent: [],
        depositAmount: '',
        bonusAmount: '',
        desc: '',
      },
      buttonHidden: false,
      parkfieldId: '',
      monitorDialogTitle: '',
      monitorDialog: false,
      playMonitorDialogTitle: '',
      playMonitorDialog: false,
      promotionDialog: false,
      lcmonitors: [],
      monitorForm: {
        monitorContent: [],
        appkey: '',
        secret:'',
        url: '',
        brand: '',
      },
      buttonHiddenMonitor: false,
      roleIndex: true,
      cellEditIndexArray: [],

      editLastTlementTimeParams: {},

      typeInit: {},
      types: [
        {
          type: 0,
          typeName: '扫码充电'
        },{
          type: 1,
          typeName: '相机联动'
        },{
          type: 2,
          typeName: '纯充电桩'
        },{
          type: 3,
          typeName: '纯泊控器'
        }
      ],

      accounts: [],
      selectDisable: false,

      hours: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24],

      rules: {
        unicode: [
            { required: true, message: '请输入停车场编号', trigger: 'blur' }
          ],
        name: [
            { required: true, message: '请输入停车场名称', trigger: 'blur' }
          ],
        address: [
            { required: true, message: '请输入停车场位置', trigger: 'blur' }
          ],
        longitude: [
            { required: true, message: '请输入经度', trigger: 'blur' }
          ],
        latitude: [
            { required: true, message: '请输入纬度', trigger: 'blur' }
          ],
        type: [
            { required: true, message: '请选择停车场类型', trigger: 'change' }
          ],
        dayFree: [
            { required: true, message: '请选择占位收费时间', trigger: 'change' }
          ],
        nightFree: [
            { required: true, message: '请选择占位收费时间', trigger: 'change' }
          ],
        managerId: [
            { required: true, message: '请选择权属人', trigger: 'change' }
          ],
      },

      promotionRules: {
        name: [
            { required: true, message: '请输入活动名称', trigger: 'blur' }
          ],
        effectiveDate: [
            { required: true, message: '请选择生效日期', trigger: 'blur' }
          ],
        deadlineDate: [
            { required: true, message: '请选择结束日期', trigger: 'blur' }
          ],
      },

    };
  },
  methods: {

    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 0) {
        return "warning-row";
      }
      return "";
    },

    delParkfield (index, row) {
      let _this = this;
      this.$confirm('确认删除停车场“' + row.name + '”', '提示', {
        type: 'warning'
      })
        .then(_ => {
          delParkfieldApi(row).then(resp => {
            if (resp.errmsg === "ok") {
              _this.loadParkfields(_this.pageNo, _this.pageSize);
              _this.$notify({
                title: "成功",
                message: "停车场删除成功",
                type: "success"
              });
            } else {
              _this.$notify({
                title: "失败",
                message: "停车场删除失败",
                type: "error"
              });
            }
          });
        })

    },

    saveParkfield () {
      let _this = this;
      console.log(_this.form)

      _this.$refs.form.validate((valid) => {
        if (valid) {
          if(_this.form.managerId == undefined ||  _this.form.managerId == '' ){
              _this.$notify({
                title: "失败",
                message: "权属人不能为空",
                type: "error"
              });
              return;
            }

            // if(_this.form.settlementAccount == undefined ||  _this.form.settlementAccount == '' ){
            //   _this.$notify({
            //     title: "失败",
            //     message: "结算账号不能为空",
            //     type: "error"
            //   });
            //   return;
            // }

            if(Number(_this.form.dayFree) > Number(_this.form.nightFree)){
              _this.$notify({
                title: "失败",
                message: "免费开始时间不能大于结束时间",
                type: "error"
              });
              return;
            }
            if(_this.form.occupiedIssuedDate != undefined && _this.form.occupiedIssuedDate != null && _this.form.occupiedIssuedDate != ''){
              _this.form.occupiedIssuedDate = _this.moment(_this.form.occupiedIssuedDate).format("YYYY-MM-DD HH:mm:ss")
            }
            if (_this.dialogTitle === "添加停车场") {
            addParkfieldApi(_this.form).then(resp => {
              _this.addDialog = false;
              if (resp.errmsg === "ok") {
                _this.loadParkfields(_this.pageNo, _this.pageSize);
                _this.$notify({
                  title: "成功",
                  message: "停车场添加成功",
                  type: "success"
                });
              } else {
                _this.$notify({
                  title: "失败",
                  message: "停车场添加失败",
                  type: "error"
                });
              }
            });
          } else {
            updateParkfieldApi(_this.form).then(resp => {
              _this.addDialog = false;
              if (resp.errmsg === "ok") {
                _this.loadParkfields(_this.pageNo, _this.pageSize);
                _this.$notify({
                  title: "成功",
                  message: "停车场修改成功",
                  type: "success"
                });
              } else {
                _this.$notify({
                  title: "失败",
                  message: "停车场修改失败",
                  type: "error"
                });
              }
            });
          }
        } else {
          return false;
        }
      });
    },


    queryBtn () {
      let _this = this
      _this.loading = true
     
      _this.loadParkfields(_this.pageNo, _this.pageSize, _this.queryParams)
      
    },

    getNowDate() {
      const timeOne = new Date()
      const year = timeOne.getFullYear()
      let month = timeOne.getMonth() + 1
      let day = timeOne.getDate()
      month = month < 10 ? '0' + month : month
      day = day < 10 ? '0' + day : day
      const NOW_MONTHS_AGO = `${year}-${month}-${day}`
      return NOW_MONTHS_AGO
    },

    addBtn () {

      this.startTime = this.getNowDate() + ' ' + '00:00:00',

    console.log(this.startTime)

      let _this = this;
      _this.$nextTick(()=>{
        if(_this.$refs.form != undefined){
          _this.$refs.form.resetFields();
        }
      })
      _this.dialogTitle = "添加停车场";
      _this.form = {
        dayFree: "8",
        nightFree: "24",
        occupiedIssuedDate:_this.startTime
      };
      loadManagerApi().then(resp => {
        _this.managers = resp.data.dataList;
      });

      
      _this.selectDisable = false;
      _this.addDialog = true;
    },
    editBtn (index, row) {
      let _this = this;
      _this.$nextTick(()=>{
        if(_this.$refs.form != undefined){
          _this.$refs.form.resetFields();
        }
      })
      _this.dialogTitle = "修改停车场";
      _this.form = row;
      loadManagerApi().then(resp => {
        _this.managers = resp.data.dataList;
      });
      _this.getAccounts(_this.form.managerId);
      _this.selectDisable = true;
      _this.addDialog = true;
    },

    changeManager(managerId){
      let _this = this
      if(_this.form.settlementAccount != null && _this.form.settlementAccount != '' && _this.form.settlementAccount != undefined){
        _this.form.settlementAccount = null;
      }
      _this.getAccounts(managerId);
    },

    getTypes(){
      let _this = this;
      _this.typeInit = _this.$store.state.parklock.type
      console.log(_this.typeInit)
      for(var i=0;i<4;i++){
        _this.types.push({
          typeName: _this.typeInit[i],
          type: i
        });
      }
      
    },
    editPromotionBtn (index, row) {
      let _this = this;
      _this.$nextTick(()=>{
        if(_this.$refs.promotionForm != undefined){
          _this.$refs.promotionForm.resetFields();
        }
      })
      _this.promotionDialogTitle = "促销活动配置";
      getLcparkfieldpromotionApi(row.id).then(resp => {
        _this.lcparkfieldpromotions = resp.data;
        _this.promotionForm.acitivityContent = resp.data;
        _this.parkfieldId = row.id
        if(_this.lcparkfieldpromotions.length>0){
          console.log(_this.lcparkfieldpromotions)
          console.log(_this.lcparkfieldpromotions.length)
          for(var i=0;i<_this.lcparkfieldpromotions.length;i++){
            // _this.addItem()
            _this.promotionForm.acitivityContent.depositAmount = _this.lcparkfieldpromotions[i].depositAmount
            _this.promotionForm.acitivityContent.bonusAmount = _this.lcparkfieldpromotions[i].bonusAmount
            _this.promotionForm.acitivityContent.desc = _this.lcparkfieldpromotions[i].desc
          }
          _this.promotionForm.name = _this.lcparkfieldpromotions[0].name
          _this.promotionForm.effectiveDate = _this.lcparkfieldpromotions[0].effectiveDate
          _this.promotionForm.deadlineDate = _this.lcparkfieldpromotions[0].deadlineDate
        }
        console.log(_this.promotionForm.acitivityContent)
        console.log(_this.promotionForm.acitivityContent.length)
        if(_this.promotionForm.acitivityContent.length==0){
          _this.promotionForm = {acitivityContent: []}
          _this.addItem()
          _this.buttonHidden = true
        }else{
          _this.buttonHidden = false
        }
        _this.promotionDialog = true;
      });
    },
    addItem () {
      let _this = this;
      if(_this.promotionForm.acitivityContent.length<4){
        _this.promotionForm.acitivityContent.push({
          depositAmount: '',
          bonusAmount: '',
          desc: '',
        })
      }else{
        _this.$notify({
          title: "",
          message: "至多配置4个优惠",
          type: "warning"
        });
      }
    },
    deleteItem () {
      let _this = this;
      if(_this.promotionForm.acitivityContent.length>1){
        _this.promotionForm.acitivityContent.splice(_this.promotionForm.acitivityContent.length-1, 1)
      }else{
        _this.$notify({
          title: "",
          message: "至少配置1个优惠",
          type: "warning"
        });
      }
    },
    delPromotionBtn () {
      let _this = this;
      _this.$confirm('确认删除该停车场所有优惠活动吗？', '提示', {
        type: 'warning'
      }).then(_ => {
          delLcparkfieldpromotionApi(_this.parkfieldId).then(resp => {
            _this.promotionDialog = false;
            if (resp.errmsg === "ok") {
              _this.loadParkfields(_this.pageNo, _this.pageSize);
              _this.$notify({
                title: "成功",
                message: "删除成功",
                type: "success"
              });
            } else {
              _this.$notify({
                title: "失败",
                message: "删除失败",
                type: "error"
              });
            }
          });
        })
    },
    addLcparkfieldpromotions () {
      let _this = this;
       _this.lcparkfieldpromotions.push({
          name: '',
          effectiveDate: '',
          deadlineDate: '',
          depositAmount: '',
          bonusAmount: '',
          desc: '',
          parkfieldId: '',
        })
 
    },
    savePromotionBtn () {
      let _this = this;
      _this.$refs.promotionForm.validate((valid) => {
        if (valid) {
   
          for(var i=0;i<_this.promotionForm.acitivityContent.length;i++){
            if(_this.lcparkfieldpromotions.length<_this.promotionForm.acitivityContent.length){
              _this.addLcparkfieldpromotions()
            }
            _this.lcparkfieldpromotions[i].name = _this.promotionForm.name
            _this.lcparkfieldpromotions[i].effectiveDate = _this.moment(_this.promotionForm.effectiveDate).format("YYYY-MM-DD HH:mm:ss")
            _this.lcparkfieldpromotions[i].deadlineDate = _this.moment(_this.promotionForm.deadlineDate).format("YYYY-MM-DD HH:mm:ss")
            _this.lcparkfieldpromotions[i].depositAmount = _this.promotionForm.acitivityContent[i].depositAmount
            _this.lcparkfieldpromotions[i].bonusAmount = _this.promotionForm.acitivityContent[i].bonusAmount 
            _this.lcparkfieldpromotions[i].desc = _this.promotionForm.acitivityContent[i].desc
            _this.lcparkfieldpromotions[i].parkfieldId = _this.parkfieldId
          }
          setLcparkfieldpromotionApi(_this.lcparkfieldpromotions).then(resp => {
            _this.promotionDialog = false;
            if(_this.buttonHidden){
              _this.buttonHidden = false
            }
            if (resp.errmsg === "ok") {
            _this.loadParkfields(_this.pageNo, _this.pageSize);
              _this.$notify({
                title: "成功",
                message: "促销活动配置成功",
                type: "success"
              });
            } else {
              _this.$notify({
                title: "失败",
                message: "促销活动配置失败",
                type: "error"
              });
            }
          });
        }
      })
    },
    editMonitorBtn (index, row) {
      let _this = this;
      _this.$nextTick(()=>{
        if(_this.$refs.monitorForm != undefined){
          _this.$refs.monitorForm.resetFields();
        }
      })
      _this.monitorDialogTitle = "监控配置";
      getLcMonitorInfoApi(row.id).then(resp => {
        _this.lcmonitors = resp.data;
        _this.monitorForm.monitorContent = resp.data;
        _this.parkfieldId = row.id
        if(_this.monitorForm.monitorContent==0){
          _this.addMonitorItem()
          _this.buttonHiddenMonitor = true
        }
        _this.monitorDialog = true;
      });
    },
    addMonitorItem () {
      let _this = this;
      if(_this.monitorForm.monitorContent.length<4){
        _this.monitorForm.monitorContent.push({
          appkey: '',
          secret: '',
          url: '',
          brand: '',
        })
      }else{
        _this.$notify({
          title: "",
          message: "至多配置4个摄像头",
          type: "warning"
        });
      }
    },
    deleteMonitorItem () {
      let _this = this;
      if(_this.monitorForm.monitorContent.length>1){
        _this.monitorForm.monitorContent.splice(_this.monitorForm.monitorContent.length-1, 1)
      }else{
        _this.$notify({
          title: "",
          message: "至少配置1个摄像头",
          type: "warning"
        });
      }
    },
    delMonitorBtn () {
      let _this = this;
      _this.$confirm('确认删除该停车场所有监控摄像头吗？', '提示', {
        type: 'warning'
      }).then(_ => {
          delLcMonitorInfoApi(_this.parkfieldId).then(resp => {
            _this.monitorDialog = false;
            if (resp.errmsg === "ok") {
              _this.loadParkfields(_this.pageNo, _this.pageSize);
              _this.$notify({
                title: "成功",
                message: "删除成功",
                type: "success"
              });
            } else {
              _this.$notify({
                title: "失败",
                message: "删除失败",
                type: "error"
              });
            }
          });
        })
    },

    saveMonitorBtn () {
      let _this = this;
      _this.$refs.monitorForm.validate((valid) => {
        if (valid) {
          for(var i=0;i<_this.monitorForm.monitorContent.length;i++){
            _this.lcmonitors[i].appkey = _this.monitorForm.monitorContent[i].appkey
            _this.lcmonitors[i].secret = _this.monitorForm.monitorContent[i].secret
            _this.lcmonitors[i].url = _this.monitorForm.monitorContent[i].url 
            _this.lcmonitors[i].brand = _this.monitorForm.monitorContent[i].brand
            _this.lcmonitors[i].parkfieldId = _this.parkfieldId
          }
          setLcMonitorInfoApi(_this.lcmonitors).then(resp => {
            _this.monitorDialog = false;
            if(_this.buttonHiddenMonitor){
              _this.buttonHiddenMonitor = false
            }
            if (resp.errmsg === "ok") {
            _this.loadParkfields(_this.pageNo, _this.pageSize);
              _this.$notify({
                title: "成功",
                message: "监控配置成功",
                type: "success"
              });
            } else {
              _this.$notify({
                title: "失败",
                message: "监控配置失败",
                type: "error"
              });
            }
          });
        }
      })
    },
   

    getAccounts(managerId){
      let _this = this;
      loadPartnerByManagerApi(managerId).then(resp => {
        _this.accounts = resp.data.dataList;
      });
    },

    handlePageChange (pageSize) {
      this.pageSize = pageSize;
      this.loadParkfields(this.pageNo, this.pageSize);
    },
    handleNoChange (pageNo) {
      this.pageNo = pageNo;
      this.loadParkfields(this.pageNo, this.pageSize);
    },
    handleTableChange (val) {
      this.multipleSelection = val;
    },
    loadParkfields (pageNo, pageSize) {
      let _this = this;
      _this.loading = true;
      loadParkfieldApi({pageNo:pageNo, pageSize:pageSize,name:_this.queryParams.name}).then(resp => {
        _this.parkfields = resp.data.dataList;
        _this.cellEditIndexArray = [];
        for(var i=0;i<_this.parkfields.length;i++){
          _this.cellEditIndexArray.push(false);
        }
   
        _this.total = resp.data.totalNum;
        _this.loading = false;
      });
    },
    handleTime(index, row) {
      let _this = this;
      console.log(index, row)
      for(var i=0;i<_this.cellEditIndexArray.length;i++){
        if(_this.cellEditIndexArray[i]){
          _this.cellEditIndexArray[i] = false;
        }
      }
      _this.cellEditIndexArray[index] = true;
      _this.reload();
    },
    reload (){
      let _this = this;
      loadParkfieldApi(_this.pageNo, _this.pageSize).then(resp => {
        _this.parkfields = resp.data.dataList;
        _this.total = resp.data.totalNum;
      });
    },
    handleTimeSub(index, row) {
      console.log(index, row);
      let _this = this;
      row.lastSettlementDate = _this.moment(row.lastSettlementDate).format("YYYY-MM-DD HH:mm:ss")
      _this.editLastTlementTimeParams = {parkfieldId: row.id, settlementTime: row.lastSettlementDate}
      updateSettlementDateByParkfield(_this.editLastTlementTimeParams).then(resp => {
        this.cellEditIndexArray[index] = false;
        this.reload();
      });
    },
    handleTimeCancel(index, row) {
      this.cellEditIndexArray[index] = false;
      this.reload();
    },
  },
  computed: {
    userState () {
      return getNickName() === getTestUserName();
    }
  },
  mounted: function () {
    this.$nextTick(function () {
      this.loadParkfields(this.pageNo, this.pageSize);
      this.roleIndex = getRoleId()==1?false:true;
    
      this.$store.dispatch("loadType");
 
    });
  },
};
</script>

<style scoped>


.el-table__body tr.current-row>td {
    background-color: #79b7fd;
}

.el-table .warning-row{
    background:#79b7fd;
  }
  .el-table--enable-row-hover .el-table__body tr:hover > td{
    background-color: #79b7fd;
    
  }

  .el-table--striped .el-table__body tr.el-table__row--striped.current-row td {
    background-color: #79b7fd;
}



.page-tool div {
  text-align: right;
}

.lock-btn {
  margin-bottom: 5px;
  margin-top: -10px;
}
.operateBtn {
  font-size: 18px;
  width: 32px;
  height: 32px;
  padding: 0;
}
</style>